package com.bdqn.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.sys.entity.Notice;
import com.bdqn.sys.entity.User;
import com.bdqn.sys.service.NoticeService;
import com.bdqn.sys.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  前端控制器 公告
 * </p>
 *
 * @author KazuGin
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/sys/notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;

    /**
     * 查询公告的列表
     * @param noticeVo
     * @return
     */
    @RequestMapping("/list")
    public DateGindViewResult  list(NoticeVo noticeVo){
        //查询分页对象
        IPage<Notice> page=new Page<Notice>(noticeVo.getPage(),noticeVo.getLimit());
        //条件
        QueryWrapper<Notice> queryWrapper=new QueryWrapper<Notice>();
        //动态条件查询
        //标题
        queryWrapper.like(StringUtils.isNoneBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        //发布人
        queryWrapper.like(StringUtils.isNoneBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        //开始时间
        queryWrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        //结束时间
        queryWrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        //按c创建时间降序
        queryWrapper.orderByDesc("createtime");

        //调用查询的方法 这里要放两个参数因为是分页的 放分页对象 还有条件
        noticeService.page(page,queryWrapper);

        //返回结果

        return  new DateGindViewResult(page.getTotal(),page.getRecords());
    }



    @PostMapping("/addNotice")
    public JSONResult addNotic(NoticeVo noticeVo, HttpSession session){
        //获取当前用户
        User user = (User) session.getAttribute(SystemConstants.LOGIN_USER);
        noticeVo.setCreatetime(new Date());//创建时间
        noticeVo.setOpername(user.getName());//发布人


        if (noticeService.save(noticeVo)){
            return  SystemConstants.Add_SUCCESS;

        }
        return  SystemConstants.Add_ERROR;




    }

    /**
     * 更新公告
     * @param noticeVo
     * @return
     */
    @PostMapping("/updateNotice")
    public  JSONResult updateNotice(NoticeVo noticeVo){
        noticeVo.setModifytime(new Date());    //更新时间
        if (noticeService.updateById(noticeVo)){
            return  SystemConstants.UPDATE_SUCCESS;
        }
        return  SystemConstants.UPDATE_ERROR;
    }


    @PostMapping("/deleteById")
    public  JSONResult deleteById(int id){
        if (noticeService.removeById(id)){
            return  SystemConstants.DELETE_SUCCESS;
        }
        return  SystemConstants.DELETE_ERROR;
    }


    @PostMapping("/deleteNoticeList")
    public  JSONResult deleteNoticeList(String ids){
        try {
            String [] idsStr=ids.split(",");
            if (noticeService.removeByIds(Arrays.asList(idsStr))){
                //删除成功
                return  SystemConstants.DELETE_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //删除失败
        return  SystemConstants.DELETE_ERROR;
    }

}

